<?php
class Admin_Form_ValidateUser extends Zendvn_Validate_Form{
	
	public function __construct($arrParam = null, $options = null){
		
		define('NAME_ID',$arrParam['id']);
		$config = new Zendvn_Controller_Config(array('module'=>$arrParam['module'],
												'fileConfig'=>'validates/user.ini'));
		if($arrParam['action']=='edit'){
			$config = new Zendvn_Controller_Config(array('module'=>$arrParam['module'],
					'fileConfig'=>'validates/user.ini',
					'section'=>'edit'));
		}
		
		//========================== email =============================
		$name  = $config->getConfig('email');
		$validator  =  $this->validate($name,$arrParam);
		if(!$validator->isValid($arrParam['email'])){
			$message = $validator->getMessages();
			$this->_arrError['email'] = current($message);
			$arrParam['email'] = '';
		}
		//========================== group_id =============================
		if(!$arrParam['group_id']){
			$this->_arrError['group_id'] = 'Please select a group.';
		}
		//========================== group_id =============================
		$error = Admin_Form_AdminUser_ValidateImage::validate($arrParam, $options);
		if($error != ''){
			$this->_arrError['avatar'] = $error;
			if($arrParam['action'] == 'edit'){
				$arrParam['avatar'] = $arrParam['current_avatar'];
			}
		}else{
			if($arrParam['action'] == 'edit'){
				$arrParam['avatar'] = $arrParam['current_avatar'];
			}
		}
		//========================== password =============================
		$flagPass = false;
		if($arrParam['action'] == 'add'){			
			$flagPass = true;
		}elseif($arrParam['action'] == 'edit'){			
			if(trim($arrParam['password'])!=''){
				$flagPass = true;
			}
		}
		if($flagPass == true){
			$name  = $config->getConfig('password');
			$validator  =  $this->validate($name,$arrParam);
			if(!$validator->isValid($arrParam['password'])){
				$message = $validator->getMessages();
				$this->_arrError['password'] = current($message);
				$arrParam['password'] = '';
			}
		}
		//========================== first_name =============================
		if($arrParam['first_name']){
			$name  = $config->getConfig('first_name');
			$validator  =  $this->validate($name,$arrParam);
			if(!$validator->isValid($arrParam['first_name'])){
				$message = $validator->getMessages();
				$this->_arrError['first_name'] = current($message);
				$arrParam['first_name'] = '';
			}
		}
		//========================== last_name =============================
		if($arrParam['last_name']){
			$name  = $config->getConfig('last_name');
			$validator  =  $this->validate($name,$arrParam);
			if(!$validator->isValid($arrParam['last_name'])){
				$message = $validator->getMessages();
				$this->_arrError['last_name'] = current($message);
				$arrParam['last_name'] = '';
			}
		}
		//========================== birthday =============================
		if($arrParam['birthday']){
			$name  = $config->getConfig('birthday');
			$validator  =  $this->validate($name,$arrParam);
			if(!$validator->isValid($arrParam['birthday'])){
				$message = $validator->getMessages();
				$this->_arrError['birthday'] = current($message);
				$arrParam['birthday'] = '';
			}
		}
		//========================== sign =============================
		if($arrParam['sign']){
			$name  = $config->getConfig('sign');
			$validator  =  $this->validate($name,$arrParam);
			if(!$validator->isValid($arrParam['sign'])){
				$message = $validator->getMessages();
				$this->_arrError['sign'] = current($message);
				$arrParam['sign'] = '';
			}
		}
		
		$this->_arrData = $arrParam;
	}
	
	public function getData(){
		if(!$this->isVaild()){
			$arrFile = $this->upload();
			if(count($arrFile)>0){
				$this->_arrData = array_merge($this->_arrData,$arrFile);
			}
		}
		return $this->_arrData;
	}
	public function isVaild(){
		$flag = false;
		if(count($this->_arrError) > 0){
			$flag = true;
		}
		return $flag;
	}
	public function getMessageErrors(){
		return $this->_arrError;
	}
	public function upload(){
		$config = new Zendvn_Controller_Config(array('module'=>$this->_arrData['module'],
													'fileConfig'=>'user.ini'));
		$itemConfig = $config->getConfig('config');
		
		$upload = new Zendvn_File_Upload();
		$fileInfo = $upload->getFileInfo('avatar');
		$fileName = $fileInfo['avatar']['name'];
	
		if(!empty($fileName)){		
			
			//===================================================================
			$options = array('custom_rename'=> true,'uploadDir'=> $itemConfig['uploadDir']);
			$fileName = $upload->upload($itemConfig['uploadDir'],'avatar',$options);
			
			$arrFile['avatar'] = $fileName;
			
			$arrParam = array(
							'uploadDir' => $itemConfig['uploadDir'],
							'thumb-width' => array($itemConfig['thumb-width1'], $itemConfig['thumb-width2'], $itemConfig['thumb-width3']),
							'thumb-height' => array($itemConfig['thumb-height1'], $itemConfig['thumb-height2'], $itemConfig['thumb-height3']),
							'thumbDir'	=> array($itemConfig['thumbDir1'], $itemConfig['thumbDir2'], $itemConfig['thumbDir3'])
							);
			
			$options = array('type' => array($itemConfig['thumbResize1'], $itemConfig['thumbResize2'], $itemConfig['thumbResize3']));
			$img = Zendvn_File_Image::resizeImg($fileName, $arrParam, $options);
			//===================================================================
			
			if($this->_arrData['action'] == 'edit'){
				$upload->remove($itemConfig['uploadDir'],$this->_arrData['current_avatar']);
				$upload->remove($itemConfig['thumbDir1'],$this->_arrData['current_avatar']);
				$upload->remove($itemConfig['thumbDir2'],$this->_arrData['current_avatar']);
				$upload->remove($itemConfig['thumbDir3'],$this->_arrData['current_avatar']);
			}
		
		}
		
		return $arrFile;
	}
}







